package scalab.caches.cep

import scalab.core.EvictionPolicy
import java.io.File

/**
*	Defines a Least Recently Used Eviction Policy.
*/
trait LRUCEP extends EvictionPolicy{

	def evictionOrder(productDirectories: Iterable[File]): Stream[File] = {
		import scala.collection.immutable.{SortedSet, TreeSet}
		val sorted: SortedSet[File] = (new TreeSet[File]()(LRUCEP.lastModTimeOrder _)) ++ productDirectories
		sorted.toStream
	}
}

object LRUCEP{
	def lastModTimeOrder(f: File): Ordered[File] = new Ordered[File]{
		def compare(that: File): Int = {
			val m1 = f.lastModified
			val m2 = that.lastModified
			if(m1 < m2)
				-1
			else if(m2 < m1)
				1
			else
				0
		}
	}
}
